#include<iostream>
#include<vector>
#include<stack>
using namespace std;

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */

struct TreeNode {
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode* left, TreeNode* right) : val(x), left(left), right(right) {}
};

class Solution {
public:
    vector<int> preorderTraversal(TreeNode* root) {
        if (root == nullptr) return {};
        stack<TreeNode*> st;
        vector<int> ans;
        // ans.push_back(root->val);
        TreeNode* pcur = root;
        while (pcur) {
            st.push(pcur);
            ans.push_back(pcur->val);
            pcur = pcur->left;
        }
        while (!st.empty() || root == pcur) {
            TreeNode* ptmp = st.top();
            st.pop();
            if (ptmp->right) {
                pcur = ptmp->right;
            }
            while (pcur) {
                st.push(pcur);
                ans.push_back(pcur->val);
                pcur = pcur->left;
            }
        }
        return ans;
    }
};


/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<int> preorderTraversal(TreeNode* root) {
        if (root == nullptr) return {};
        stack<TreeNode*> st;
        vector<int> ans;
        // ans.push_back(root->val);
        TreeNode* pcur = root;
        while (!st.empty() || pcur) {
            while (pcur) {
                st.push(pcur);
                ans.push_back(pcur->val);
                pcur = pcur->left;
            }
            TreeNode* ptmp = st.top();
            st.pop();
            pcur = ptmp->right;
        }
        return ans;
    }
};

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<int> inorderTraversal(TreeNode* root) {
        if (root == nullptr) return {};
        stack<TreeNode*> st;
        vector<int> ans;
        // ans.push_back(root->val);
        TreeNode* pcur = root;
        while (!st.empty() || pcur) {
            while (pcur) {
                st.push(pcur);
                pcur = pcur->left;
            }
            TreeNode* ptmp = st.top();
            st.pop();
            ans.push_back(ptmp->val);
            pcur = ptmp->right;
        }
        return ans;
    }
};

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<int> postorderTraversal(TreeNode* root) {
        if (root == nullptr) return {};
        stack<TreeNode*> st;
        stack<TreeNode*> st_rem;
        vector<int> ans;
        // ans.push_back(root->val);
        TreeNode* pcur = root;
        while (!st.empty() || pcur) {
            while (pcur) {
                st.push(pcur);
                pcur = pcur->left;
            }
            TreeNode* ptmp = st.top();
            // st.pop();
            // ans.push_back(ptmp->val);
            if (ptmp->right == nullptr || (!st.empty() && !st_rem.empty() && st_rem.top() == st.top()->right)) {
                ans.push_back(ptmp->val);
                st_rem.push(ptmp);
                st.pop();
            }
            else pcur = ptmp->right;
        }
        return ans;
    }

};